package Veterinaria.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import Veterinaria.excepcion.CodigoDuplicadoExcepcion;
import Veterinaria.excepcion.DAOExcepcion;
import Veterinaria.modelo.Articulo;
import Veterinaria.negocio.GestionArticulo;

/**
 * Servlet implementation class ArticuloServlets
 */
@WebServlet("/ArticuloServlets")
public class ArticuloServlets extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticuloServlets() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Entro");
		PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");
        
        System.out.println("Servlets");
        JSONArray arrayObj = new JSONArray();
        JSONObject obj1 = new JSONObject();
        System.out.println("Servlets1");
        	
		GestionArticulo articulo = new GestionArticulo();
		String des1 = null;
		//String y = null;
		try {
			System.out.println("Servlets2");
			String query = request.getParameter("term");
	        System.out.println(query);
	        query = query.toLowerCase();
	        
			Collection<Articulo> articulos = articulo.ListaArticuloXDescripcion(query);

			for (Articulo art : articulos) {
				if (art.getDescripcion().toLowerCase().startsWith(query)) des1 = art.getDescripcion();
				obj1.put("label", des1);
				obj1.put("value", des1);
				obj1.put("id", art.getCod_articulo());
				obj1.put("uni", art.getUni_med());
				obj1.put("pre", art.getPrecio());
				
				arrayObj.add(obj1.toString());
				obj1.clear();				
			}

		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	        out.println(arrayObj);
	        out.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String x = request.getParameter("txtAccion");
		
		String descripcion = request.getParameter("descripcion");
		String unidaddemedida = request.getParameter("unidaddemedida");
		String precio = request.getParameter("precio");
		String idarticulo = request.getParameter("idarticulo");
		
		double xprecio = Double.parseDouble(precio);
		
		GestionArticulo art = new GestionArticulo();
		
		if(x.equals("INSERTAR")){
			Articulo codigo = null;
			try {
				codigo = art.GenerarCodigo();
			} catch (DAOExcepcion e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			try {
				//art.insertar("A0011", descripcion, unidaddemedida, xprecio, "1");
				art.insertar(codigo.getCod_articulo(), descripcion, unidaddemedida, xprecio, "1");
			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				request.setAttribute("Mensaje", e);
			} catch (CodigoDuplicadoExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				request.setAttribute("Mensaje", e);
			}
			RequestDispatcher rd = request.getRequestDispatcher("articulos.jsp");
			rd.forward(request, response);	
		}
		
		else if(x.equals("MODIFICAR")){
			try {
				//art.actualizar("A0003", descripcion, unidaddemedida, xprecio, "1");
				art.actualizar(idarticulo, descripcion, unidaddemedida, xprecio, "1");
			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			RequestDispatcher rd = request.getRequestDispatcher("modificar_articulos.jsp");
			rd.forward(request, response);
		}
		
		else if(x.equals("ELIMINAR")){
			try {
				//art.eliminar("A0002");
				art.eliminar(idarticulo);
			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			RequestDispatcher rd = request.getRequestDispatcher("eliminar_articulos.jsp");
			rd.forward(request, response);
		}
	
	}
}